package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPig;

import cn.edu.scau.cmi.hombio.service.TaskPigService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for TaskPig entities
 * 
 */

@Controller("TaskPigRestController")

public class TaskPigRestController {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPig entities
	 * 
	 */
	@Autowired
	private TaskPigDAO taskPigDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for TaskPig entities
	 * 
	 */
	@Autowired
	private TaskPigService taskPigService;

	/**
	 * Get Pig entity by TaskPig
	 * 
	 */
	@RequestMapping(value = "/TaskPig/{taskpig_id}/pig", method = RequestMethod.GET)
	@ResponseBody
	public Pig getTaskPigPig(@PathVariable Integer taskpig_id) {
		return taskPigDAO.findTaskPigByPrimaryKey(taskpig_id).getPig();
	}

	/**
	* Show all TaskPig entities
	* 
	*/
	@RequestMapping(value = "/TaskPig", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPig> listTaskPigs() {
		return new java.util.ArrayList<TaskPig>(taskPigService.loadTaskPigs());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Get Task entity by TaskPig
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getTaskPigTask(@PathVariable Integer taskpig_id) {
		return taskPigDAO.findTaskPigByPrimaryKey(taskpig_id).getTask();
	}

	/**
	* Delete an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/pig/{pig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigPig(@PathVariable Integer taskpig_id, @PathVariable Integer related_pig_id) {
		taskPigService.deleteTaskPigPig(taskpig_id, related_pig_id);
	}

	/**
	* View an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/pig/{pig_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pig loadTaskPigPig(@PathVariable Integer taskpig_id, @PathVariable Integer related_pig_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		return pig;
	}

	/**
	* Create a new Pig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/pig", method = RequestMethod.POST)
	@ResponseBody
	public Pig newTaskPigPig(@PathVariable Integer taskpig_id, @RequestBody Pig pig) {
		taskPigService.saveTaskPigPig(taskpig_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPigTask(@PathVariable Integer taskpig_id, @PathVariable Integer related_task_id) {
		taskPigService.deleteTaskPigTask(taskpig_id, related_task_id);
	}

	/**
	* Save an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/pig", method = RequestMethod.PUT)
	@ResponseBody
	public Pig saveTaskPigPig(@PathVariable Integer taskpig_id, @RequestBody Pig pig) {
		taskPigService.saveTaskPigPig(taskpig_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTaskPigTask(@PathVariable Integer taskpig_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Delete an existing TaskPig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskPig(@PathVariable Integer taskpig_id) {
		TaskPig taskpig = taskPigDAO.findTaskPigByPrimaryKey(taskpig_id);
		taskPigService.deleteTaskPig(taskpig);
	}

	/**
	* Select an existing TaskPig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPig loadTaskPig(@PathVariable Integer taskpig_id) {
		return taskPigDAO.findTaskPigByPrimaryKey(taskpig_id);
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTaskPigTask(@PathVariable Integer taskpig_id, @RequestBody Task task) {
		taskPigService.saveTaskPigTask(taskpig_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/TaskPig/{taskpig_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newTaskPigTask(@PathVariable Integer taskpig_id, @RequestBody Task task) {
		taskPigService.saveTaskPigTask(taskpig_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Create a new TaskPig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig", method = RequestMethod.POST)
	@ResponseBody
	public TaskPig newTaskPig(@RequestBody TaskPig taskpig) {
		taskPigService.saveTaskPig(taskpig);
		return taskPigDAO.findTaskPigByPrimaryKey(taskpig.getId());
	}

	/**
	* Save an existing TaskPig entity
	* 
	*/
	@RequestMapping(value = "/TaskPig", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPig saveTaskPig(@RequestBody TaskPig taskpig) {
		taskPigService.saveTaskPig(taskpig);
		return taskPigDAO.findTaskPigByPrimaryKey(taskpig.getId());
	}
}